Passed
Pull Request — master (#406)
by
unknown
01:44
created

EditEventController.get   A

Complexity

Conditions 1

Size

Total Lines 34
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 30
dl 0
loc 34
rs 9.16
c 0
b 0
f 0
1
import {
2
  BadRequestException,
3
  Body,
4
  Controller,
5
  Get,
6
  Inject,
7
  Param,
8
  Post,
9
  Render,
10
  Res,
11
  UseGuards
12
} from '@nestjs/common';
13
import { ICommandBus } from 'src/Application/ICommandBus';
14
import { IsAuthenticatedGuard } from 'src/Infrastructure/HumanResource/User/Security/IsAuthenticatedGuard';
15
import { WithName } from 'src/Infrastructure/Common/ExtendedRouting/WithName';
16
import { AddEventControllerDTO } from '../DTO/AddEventControllerDTO';
17
import { LoggedUser } from 'src/Infrastructure/HumanResource/User/Decorator/LoggedUser';
18
import { User } from 'src/Domain/HumanResource/User/User.entity';
19
import { IQueryBus } from 'src/Application/IQueryBus';
20
import { GetTasksQuery } from 'src/Application/Task/Query/GetTasksQuery';
21
import { GetProjectsQuery } from 'src/Application/Project/Query/GetProjectsQuery';
22
import { Pagination } from 'src/Application/Common/Pagination';
23
import { ProjectView } from 'src/Application/Project/View/ProjectView';
24
import { TaskView } from 'src/Application/Task/View/TaskView';
25
import { Response } from 'express';
26
import { GetCooperativeQuery } from 'src/Application/Settings/Query/GetCooperativeQuery';
27
import { CooperativeView } from 'src/Application/Settings/View/CooperativeView';
28
import { ArrayUtils } from 'src/Infrastructure/Common/Utils/ArrayUtils';
29
import { IdDTO } from 'src/Infrastructure/Common/DTO/IdDTO';
30
import { EditEventDTO } from '../DTO/EditEventDTO';
31
import { UpdateEventCommand } from 'src/Application/FairCalendar/Command/UpdateEventCommand';
32
import { GetEventByIdQuery } from 'src/Application/FairCalendar/Query/GetEventByIdQuery';
33
34
@Controller('app/faircalendar/events/edit')
35
@UseGuards(IsAuthenticatedGuard)
36
export class EditEventController {
37
  constructor(
38
    @Inject('ICommandBus')
39
    private readonly commandBus: ICommandBus,
40
    @Inject('IQueryBus')
41
    private readonly queryBus: IQueryBus
42
  ) {}
43
44
  @Get(':id')
45
  @WithName('faircalendar_events_edit')
46
  @Render('pages/faircalendar/events/edit.njk')
47
  public async get(@Param() idDto: IdDTO) {
48
    const event = await this.queryBus.execute(new GetEventByIdQuery(idDto.id));
49
50
    const types = [
51
      'mission',
52
      'dojo',
53
      'support',
54
      'formationConference',
55
      'other'
56
    ];
57
58
    const tasksPagination: Pagination<TaskView> = await this.queryBus.execute(
59
      new GetTasksQuery(1)
60
    );
61
62
    const projectsPagination: Pagination<ProjectView> = await this.queryBus.execute(
63
      new GetProjectsQuery(1)
64
    );
65
66
    const { dayDuration }: CooperativeView = await this.queryBus.execute(
67
      new GetCooperativeQuery()
68
    );
69
    const times = [...ArrayUtils.range(30, dayDuration, 30)].reverse();
70
71
    return {
72
      event,
73
      types,
74
      tasks: tasksPagination.items,
75
      projects: projectsPagination.items,
76
      times
77
    };
78
  }
79
80
  @Post(':id')
81
  public async post(
82
    @Param() idDto: IdDTO,
83
    @Body() dto: EditEventDTO,
84
    @LoggedUser() user: User,
85
    @Res() res: Response
86
  ) {
87
    const { type, time, summary, projectId, taskId } = dto;
88
89
    try {
90
      await this.commandBus.execute(
91
        new UpdateEventCommand(
92
          idDto.id,
93
          user,
94
          type,
95
          Number(time),
96
          projectId,
97
          taskId,
98
          summary
99
        )
100
      );
101
102
      res.redirect(303, '/app/faircalendar');
103
    } catch (e) {
104
      throw new BadRequestException(e.message);
105
    }
106
  }
107
}
108